﻿Imports System.IO
Imports System.Xml

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class Main_Form

    Const Program_Version = "0.0.0.1"
    Const AppName = "Comsa-XML-TableReader"
    Const HelpFile = "Comsa_Help_Infos.rtf"

    Dim Logfile As String = String.Empty
    Dim XMLReader As Xml.XmlReader
    Dim xml_DataSet As New DataSet("Tables")
    Dim CurrendTableName As String = "Connectors"

    ''' <summary>
    ''' Mapping Structure Help Copy Felds to other DataSet
    ''' </summary>
    ''' <remarks></remarks>
    Structure Fields
        Public Field_Colum_Name As String
        Public Field_Id As Integer
        Public BasisTable_Id As String
        Public BasisTable_Column As String
    End Structure

    Dim Field_Exist As Fields()

    ''' <summary>
    '''  Call HelpFile Class
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Help_With_F1()
        Dim HelpCall As New get_Help(HelpFile)
    End Sub

    ''' <summary>
    ''' Setup MainForm
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Main_Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Create_DataSet_Tables()
        Setup_App()
    End Sub


    Private Sub Setup_App()
        Me.Text = AppName & " Version : " & Program_Version
        create_Menue_Tables_Items()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub create_Menue_Tables_Items()

        Dim i As Integer
        For i = 0 To xml_DataSet.Tables.Count - 1
            tsMenue_SelectTable.Items.Add(xml_DataSet.Tables(i).TableName)
        Next

    End Sub


    Private Sub Create_DataSet_Tables()
        xml_DataSet.Tables.Add("FusePMDs")
        xml_DataSet.Tables.Add("RelayPMDs")
        xml_DataSet.Tables.Add("AccessoryPMDs")
        xml_DataSet.Tables.Add("AssemblyPartPMDs")

        xml_DataSet.Tables.Add("HarnessConfigurationPMDs")
        xml_DataSet.Tables.Add("WiringGroupPMDs")
        xml_DataSet.Tables.Add("ConnectorPMDs")
        xml_DataSet.Tables.Add("FixingPMDs")


        xml_DataSet.Tables.Add("GeneralSpecialWirePMDs")
        xml_DataSet.Tables.Add("GeneralWirePMDs")
        xml_DataSet.Tables.Add("PartPMDs")
        xml_DataSet.Tables.Add("SealPMDs")

        xml_DataSet.Tables.Add("PlugPMDs")
        xml_DataSet.Tables.Add("TerminalPMDs")
        xml_DataSet.Tables.Add("TapePMDs")
        xml_DataSet.Tables.Add("SymbolPMDs")

        xml_DataSet.Tables.Add("Connectors")
        xml_DataSet.Tables.Add("Fixings")
    End Sub


    Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles tsFile_OpenXML.Click
        Call Open_XML_File()
    End Sub

    ''' <summary>
    ''' Open XML-File with Dialog
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Open_XML_File()
        Dim i As Integer
        Try

            Dim openFileDialog1 As New OpenFileDialog
            openFileDialog1.Filter = "xml-File|*.xml"
            openFileDialog1.Title = "Open as XML-File"
            openFileDialog1.AddExtension = True
            openFileDialog1.DefaultExt = "xml"
            openFileDialog1.CheckFileExists = True
            openFileDialog1.Multiselect = False

            openFileDialog1.InitialDirectory = get_DesktopPath()


            If (openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then


                Read_XML(openFileDialog1.FileName)

                'Final Setup Menue
                If xml_DataSet.Tables(CurrendTableName).Rows.Count > 0 Then
                    Me.dgv.DataSource = xml_DataSet.Tables(CurrendTableName)
                Else
                    For i = 0 To xml_DataSet.Tables.Count - 1
                        If xml_DataSet.Tables(CurrendTableName).Rows.Count > 0 Then
                            Me.dgv.DataSource = xml_DataSet.Tables(i)
                            CurrendTableName = xml_DataSet.Tables(i).TableName
                            Exit For
                        End If
                    Next
                End If

                Create_Stecker_OrderTable()
                Me.tsStatusLabel.Text = CurrendTableName

                Change_to_Currend_Table("BMW-GrobZeichen")
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Read-XML", MessageBoxButtons.OK)
        End Try
    End Sub




    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="xml_FileName"></param>
    ''' <remarks></remarks>
    Private Sub Read_XML(xml_FileName As String)

        Try
            Dim doc As New XmlDocument()
            doc.Load(xml_FileName)
            Dim root As XmlNode = doc.DocumentElement

            Dim nodeList_PMD As XmlNodeList = root.SelectNodes("PMDs/*")
            For Each xmlElement As XmlNode In nodeList_PMD

                If xml_DataSet.Tables.Contains(xmlElement.Name) Then
                    Fill_DataSet(xmlElement.Name, xmlElement)
                End If
            Next

            Dim nodeList_Harness As XmlNodeList = root.SelectNodes("Harness/*")
            For Each xmlElement As XmlNode In nodeList_Harness

                If xml_DataSet.Tables.Contains(xmlElement.Name) Then
                    Fill_DataSet(xmlElement.Name, xmlElement)
                End If
            Next

            remove_Table_Items()
            Menue_Items_Switch()

            Make_GrobZeichen_Table()

            Me.tsFile_OpenXML.Visible = False
            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Read-XML", MessageBoxButtons.OK)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub remove_Table_Items()
        Try
            Dim i As Integer

            For i = 0 To xml_DataSet.Tables.Count - 1
                If xml_DataSet.Tables(i).Rows.Count = 0 Then
                    tsMenue_SelectTable.Items.Remove(xml_DataSet.Tables(i).TableName)
                End If
            Next

            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Read-XML-remove_Items", MessageBoxButtons.OK)
        End Try
    End Sub


    ''' <summary>
    '''  Switch MenueItems
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Menue_Items_Switch()
        Try
            Me.tsMenue_SelectTable.Enabled = True
            Me.tsMenue_ExportToExcel.Enabled = True
            Me.tsMenue_ExportToCSVFile.Enabled = True
            Me.tsMenue_ExportAllTablesToExcel.Enabled = True
            Me.tsMenue_TablesCopyToClipboard.Enabled = True
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error: MenueItems_Switch", MessageBoxButtons.OK)
        End Try
    End Sub



    Private Sub Create_Stecker_OrderTable()
        Const New_OrderTable = "Plugs-Table"
        Const New_GrobZeichenTable = "BMW-GrobZeichen"
        Dim count_of_Parts As Integer
        Dim LastPartId As String = String.Empty
        Dim PartId As String = String.Empty
        Dim Value As String = String.Empty

        Try
            If xml_DataSet.Tables(New_GrobZeichenTable).Rows.Count > 0 And xml_DataSet.Tables(New_GrobZeichenTable).Columns.Count > 0 Then


                'xml_DataSet.Tables.
                sort_DataTable(New_GrobZeichenTable, "BMW-ID")

                'New Table  
                xml_DataSet.Tables.Add(New_OrderTable)

                xml_DataSet.Tables(New_OrderTable).Columns.Add("Count", Type.GetType("System.Int16"))
                xml_DataSet.Tables(New_OrderTable).Columns.Add("BMW-ID", Type.GetType("System.String"))

                xml_DataSet.Tables(New_OrderTable).Columns.Add("Description", Type.GetType("System.String"))
                xml_DataSet.Tables(New_OrderTable).Columns.Add("HousingColour", Type.GetType("System.String"))

                xml_DataSet.Tables(New_OrderTable).Columns.Add("see GrobZeichen", Type.GetType("System.String"))
                xml_DataSet.Tables(New_OrderTable).Columns.Add("used by", Type.GetType("System.String"))

                'Fill GobZeichen
                For iRow = 0 To xml_DataSet.Tables(New_GrobZeichenTable).Rows.Count - 1

                    PartId = xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("BMW-ID").ToString

                    If LastPartId <> PartId Then
                        count_of_Parts = 1
                        xml_DataSet.Tables(New_OrderTable).Rows.Add()

                        ' count
                        xml_DataSet.Tables(New_OrderTable).Rows(xml_DataSet.Tables(New_OrderTable).Rows.Count - 1).Item("Count") = count_of_Parts

                        '"BMW-ID"
                        Value = xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("BMW-ID").ToString.Trim
                        xml_DataSet.Tables(New_OrderTable).Rows(xml_DataSet.Tables(New_OrderTable).Rows.Count - 1).Item("BMW-ID") = Value

                        '"Description"
                        Value = xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("Description").ToString.Trim
                        xml_DataSet.Tables(New_OrderTable).Rows(xml_DataSet.Tables(New_OrderTable).Rows.Count - 1).Item("Description") = Value

                        '"HousingColour"
                        Value = xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("HousingColour").ToString.Trim
                        xml_DataSet.Tables(New_OrderTable).Rows(xml_DataSet.Tables(New_OrderTable).Rows.Count - 1).Item("HousingColour") = Value

                        '"see GrobZeichen"
                        Value = xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("Grobzeichen").ToString.Trim
                        xml_DataSet.Tables(New_OrderTable).Rows(xml_DataSet.Tables(New_OrderTable).Rows.Count - 1).Item("see GrobZeichen") = Value

                        '"used by"
                        Value = xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("Comment").ToString.Trim
                        xml_DataSet.Tables(New_OrderTable).Rows(xml_DataSet.Tables(New_OrderTable).Rows.Count - 1).Item("used by") = Value

                        LastPartId = PartId
                    Else
                        count_of_Parts = count_of_Parts + 1
                        xml_DataSet.Tables(New_OrderTable).Rows(xml_DataSet.Tables(New_OrderTable).Rows.Count - 1).Item("Count") = count_of_Parts
                        LastPartId = PartId
                    End If
                Next

                'insert Table
                If xml_DataSet.Tables(New_OrderTable).Rows.Count > 0 Then
                    tsMenue_SelectTable.Items.Add(New_OrderTable)
                    sort_DataTable(New_GrobZeichenTable, "Grobzeichen")
                    sort_DataTable(New_OrderTable, "BMW-ID")
                End If


            Else

            End If
            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error: Create: Plugs-Oder-Table", MessageBoxButtons.OK)
        End Try
    End Sub



    ''' <summary>
    '''  Create GrobZeichenTable 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Make_GrobZeichen_Table()
        Const New_GrobZeichenTable = "BMW-GrobZeichen"
        Const TableWith_GrobZeichen = "Connectors"
        Const TableWith_ContorInfos = "ConnectorPMDs"

        Dim iRow As Integer
        Dim iColum As Integer
        Dim iBMW_Row As Integer

        Dim Value As String
        Dim Key_Value As String

        Dim maxColums_GrobZeichen As Integer
        Dim maxColums_ContorInfos As Integer
        Try


            If xml_DataSet.Tables("Connectors").Rows.Count > 0 And xml_DataSet.Tables("Connectors").Columns.Count > 0 Then

                'New Table  
                xml_DataSet.Tables.Add(New_GrobZeichenTable)


                If Add_Columns_NewTable(TableWith_GrobZeichen, "ElementID", "Grobzeichen", New_GrobZeichenTable) Then
                    maxColums_GrobZeichen = maxColums_GrobZeichen + 1
                End If

                If Add_Columns_NewTable(TableWith_GrobZeichen, "PMD", "BMW-ID", New_GrobZeichenTable) Then
                    maxColums_GrobZeichen = maxColums_GrobZeichen + 1
                End If

                If Add_Columns_NewTable(TableWith_GrobZeichen, "Comment", "Comment", New_GrobZeichenTable) Then
                    maxColums_GrobZeichen = maxColums_GrobZeichen + 1
                End If


                If Add_Columns_NewTable(TableWith_ContorInfos, "Description", "Description", New_GrobZeichenTable) Then
                    maxColums_ContorInfos = maxColums_ContorInfos + 1
                End If

                If Add_Columns_NewTable(TableWith_ContorInfos, "HousingColour", "HousingColour", New_GrobZeichenTable) Then
                    maxColums_ContorInfos = maxColums_ContorInfos + 1
                End If



                If Add_Columns_NewTable(TableWith_GrobZeichen, "TableX", "Table:X", New_GrobZeichenTable) Then
                    maxColums_GrobZeichen = maxColums_GrobZeichen + 1
                End If

                If Add_Columns_NewTable(TableWith_GrobZeichen, "TableY", "Table:Y", New_GrobZeichenTable) Then
                    maxColums_GrobZeichen = maxColums_GrobZeichen + 1
                End If



                'Fill GobZeichen
                For iRow = 0 To xml_DataSet.Tables("Connectors").Rows.Count - 1

                    xml_DataSet.Tables(New_GrobZeichenTable).Rows.Add()

                    For iColum = 0 To Field_Exist.GetUpperBound(0)

                        If Field_Exist(iColum).BasisTable_Id = TableWith_GrobZeichen Then

                            Value = xml_DataSet.Tables(TableWith_GrobZeichen).Rows(iRow).Item(Field_Exist(iColum).BasisTable_Column).ToString

                            xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item(Field_Exist(iColum).Field_Id) = Value
                        End If
                    Next


                Next

                'Fill TableWith_ContorInfos by GobZeichen
                For iRow = 0 To xml_DataSet.Tables("Connectors").Rows.Count - 1

                    For iColum = 0 To Field_Exist.GetUpperBound(0) - 1
                        If Field_Exist(iColum).BasisTable_Id = TableWith_ContorInfos Then

                            ' get BMW-Id
                            Key_Value = xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("BMW-ID").ToString.Trim

                            'Get Infos from  TableWith_ContorInfos
                            For iBMW_Row = 0 To xml_DataSet.Tables(TableWith_ContorInfos).Rows.Count - 1

                                'ID
                                If Key_Value = xml_DataSet.Tables(TableWith_ContorInfos).Rows(iBMW_Row).Item("ID").ToString Then

                                    'set "Description"
                                    Value = xml_DataSet.Tables(TableWith_ContorInfos).Rows(iBMW_Row).Item("Description").ToString
                                    xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("Description") = Value

                                    'set "HousingColour"
                                    Value = xml_DataSet.Tables(TableWith_ContorInfos).Rows(iBMW_Row).Item("HousingColour").ToString
                                    xml_DataSet.Tables(New_GrobZeichenTable).Rows(iRow).Item("HousingColour") = Value


                                    Exit For
                                End If
                            Next


                        End If
                    Next


                Next

                sort_DataTable(New_GrobZeichenTable, "Grobzeichen")

                tsMenue_SelectTable.Items.Add(New_GrobZeichenTable)

            End If
            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error: Make_GrobZeichen_Table", MessageBoxButtons.OK)
        End Try
    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="TableName"></param>
    ''' <param name="ColumName"></param>
    ''' <remarks></remarks>
    Private Sub sort_DataTable(TableName As String, ColumName As String)
        Dim sortkey As String
        Dim Sorted_Table As DataTable
        Try

            sortkey = ColumName & " ASC"
            xml_DataSet.Tables(TableName).DefaultView.Sort = sortkey


            Sorted_Table = xml_DataSet.Tables(TableName).DefaultView.ToTable()
            xml_DataSet.Tables.Remove(TableName)
            xml_DataSet.Tables.Add(Sorted_Table)

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error: sort_DataTable", MessageBoxButtons.OK)
        End Try

    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Exist_ColumName"></param>
    ''' <param name="Exist_TableName"></param>
    ''' <param name="New_GrobZeichenName"></param>
    ''' <param name="New_GrobZeichenTable"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Add_Columns_NewTable(Exist_TableName As String, Exist_ColumName As String, New_GrobZeichenName As String, New_GrobZeichenTable As String) As Boolean
        Dim Size As Integer
        Try

            If xml_DataSet.Tables(Exist_TableName).Columns.Contains(Exist_ColumName) Then

                If Add_DataTable_Column(New_GrobZeichenTable, New_GrobZeichenName) Then


                    If Not IsNothing(Field_Exist) Then
                        Size = Field_Exist.GetUpperBound(0) + 1
                    End If

                    ReDim Preserve Field_Exist(Size)

                    Field_Exist(Field_Exist.GetUpperBound(0)).Field_Colum_Name = New_GrobZeichenName
                    Field_Exist(Field_Exist.GetUpperBound(0)).Field_Id = Field_Exist.GetUpperBound(0)
                    Field_Exist(Field_Exist.GetUpperBound(0)).BasisTable_Id = Exist_TableName
                    Field_Exist(Field_Exist.GetUpperBound(0)).BasisTable_Column = Exist_ColumName
                End If

                Return True

            End If

            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="TableName"></param>
    ''' <param name="xmlNodeList"></param>
    ''' <remarks></remarks>
    Private Sub Fill_DataSet(TableName As String, xmlNodeList As XmlNode)
        Try
            For Each xmlElement As XmlNode In xmlNodeList
                'get Sub-Elemets
                Fill_DataTable(TableName, xmlElement)
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Fill_DataSet Table: " & TableName, MessageBoxButtons.OK)
        End Try
    End Sub



    Private Sub Fill_DataTable(TableName As String, xmlNodeList As XmlNode)
        Dim XML_Name As String
        Try
            XML_Name = TableName.Remove(TableName.Length - 1, 1)
            If XML_Name = xmlNodeList.Name Then
                eval_Atributes(TableName, xmlNodeList.Attributes)
            End If
            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "add_Data Table: " & TableName, MessageBoxButtons.OK)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="TableName"></param>
    ''' <param name="xmlAttributes"></param>
    ''' <remarks></remarks>
    Private Sub eval_Atributes(TableName As String, xmlAttributes As XmlAttributeCollection)
        Dim AttributName As String
        Dim AttributValue As String
        Dim curRow As Integer

        If xmlAttributes.Count > 0 Then

            xml_DataSet.Tables(TableName).Rows.Add()
            curRow = xml_DataSet.Tables(TableName).Rows.Count - 1

            For Each xmlAttribut As XmlAttribute In xmlAttributes
                AttributName = xmlAttribut.Name
                AttributValue = xmlAttribut.Value

                If AttributName.Length > 0 Then
                    Add_DateTable_Item(TableName, curRow, AttributName, AttributValue)
                End If
            Next
        End If

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="TableName"></param>
    ''' <param name="iRow"></param>
    ''' <param name="ColumnName"></param>
    ''' <param name="Value"></param>
    ''' <remarks></remarks>
    Private Sub Add_DateTable_Item(TableName As String, iRow As Integer, ColumnName As String, Value As String)
        Dim Status As Boolean
        Try
            If Not xml_DataSet.Tables(TableName).Columns.Contains(ColumnName) Then
                Status = Add_DataTable_Column(TableName, ColumnName)
            Else
                Status = True
            End If

            If Status And Value.Length > 0 Then
                xml_DataSet.Tables(TableName).Rows(iRow).Item(ColumnName) = Value
            End If
            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "add_Data Table: " & TableName, MessageBoxButtons.OK)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="TableName"></param>
    ''' <param name="ColumnName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Add_DataTable_Column(TableName As String, ColumnName As String) As Boolean
        Try
            xml_DataSet.Tables(TableName).Columns.Add(ColumnName, Type.GetType("System.String"))
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function


    Private Sub ExportToExcelToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles tsMenue_ExportToExcel.Click
        Call Dgv_Excel_Export()
    End Sub


    Private Sub Dgv_Excel_Export()
        Dim export_csv As New Dgv_Direct_to_Excel(dgv, CurrendTableName)
        'If export_csv.Export_OK Then
        '    MessageBox.Show(" Export successful! ", "Export as csv-File", MessageBoxButtons.OK)
        'End If
    End Sub


    Private Sub ExportToCSVFileToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles tsMenue_ExportToCSVFile.Click
        Dgv_Csv_Export()
    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Dgv_Csv_Export()
        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "csv-File|*.csv"
        saveFileDialog1.Title = "Save as Csv-File"
        saveFileDialog1.AddExtension = True
        saveFileDialog1.DefaultExt = "csv"
        saveFileDialog1.FileName = "Export.csv"
        saveFileDialog1.InitialDirectory = get_DesktopPath()


        If (saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Dim export_csv As New Export_DGV_to_csv(Me.dgv, saveFileDialog1.FileName)
            'If export_csv.Export_OK Then
            '    MessageBox.Show(" Export successful! ", "Export as csv-File", MessageBoxButtons.OK)
            'End If
        End If
    End Sub


    Private Sub tsMenue_ExportAllTablesToExcel_Click(sender As System.Object, e As System.EventArgs) Handles tsMenue_ExportAllTablesToExcel.Click
        Call DataSet_Excel_Export()
    End Sub


    Private Sub DataSet_Excel_Export()
        Dim export_DataSet As New DataSet_To_Excel(xml_DataSet)
    End Sub

    Private Sub update_TableItems()
        Dim NewTableName As String = String.Empty
        Try
            NewTableName = Me.tsMenue_SelectTable.SelectedItem.ToString

            If xml_DataSet.Tables(NewTableName).Rows.Count > 1 Then
                Me.dgv.DataSource = xml_DataSet.Tables(NewTableName)
                CurrendTableName = NewTableName
                Me.tsStatusLabel.Text = CurrendTableName
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "update_TableItems: " & NewTableName, MessageBoxButtons.OK)
        End Try
    End Sub

    Private Sub SelectTableToolStripMenuItem_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles tsMenue_SelectTable.SelectedIndexChanged
        update_TableItems()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="TableName"></param>
    ''' <remarks></remarks>
    Private Sub Change_to_Currend_Table(TableName As String)
        Try
            Me.dgv.DataSource = xml_DataSet.Tables(TableName)
            Me.tsStatusLabel.Text = TableName
        Catch ex As Exception
            MessageBox.Show(ex.Message, "ChangeCurrend_Table: " & TableName, MessageBoxButtons.OK)
        End Try
    End Sub


    Private Sub ts_Info_Click(sender As System.Object, e As System.EventArgs) Handles ts_Info.Click
        MessageBox.Show("Entwickler: Andreas Poppe" + Chr(13) + "Please don't use this Program for productive Application!", AppName, MessageBoxButtons.OK)
    End Sub


    Private Function get_DesktopPath() As String
        Try
            Return Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function


    ''' <summary>
    '''  Events MainForm F1 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Main_Form_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        If (e.KeyCode = Keys.F1) Then
            Help_With_F1()
            Exit Sub
        End If

        If (e.Control And e.KeyCode = Keys.C) Then
            Call Copy_DataGridView_To_Clipboard(Me.dgv, True)
            Exit Sub
        End If

    End Sub

    ''' <summary>
    ''' Menue Calls Help
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ts_Help_Click(sender As System.Object, e As System.EventArgs) Handles ts_Help.Click
        Help_With_F1()
    End Sub

    Private Sub CopyToClipboardToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles tsMenue_TablesCopyToClipboard.Click
        Call Copy_DataGridView_To_Clipboard(Me.dgv, True)
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="dgv"></param>
    ''' <param name="withColumnHeader"></param>
    ''' <remarks></remarks>
    Private Sub Copy_DataGridView_To_Clipboard(ByRef dgv As DataGridView, Optional withColumnHeader As Boolean = True)
        Dim Clip_Board_Text As String = String.Empty

        Try

            'No empty Grid
            If dgv.ColumnCount > 0 Then

                Dim oCurrent_dgv_Col As DataGridViewColumn
                oCurrent_dgv_Col = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)

                'Get ColumnHeaders
                If withColumnHeader = True Then

                    Do
                        Clip_Board_Text = Clip_Board_Text & oCurrent_dgv_Col.HeaderText & Chr(Keys.Tab)
                        oCurrent_dgv_Col = dgv.Columns.GetNextColumn(oCurrent_dgv_Col, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
                    Loop Until oCurrent_dgv_Col Is Nothing

                    Clip_Board_Text = Clip_Board_Text.Substring(0, Clip_Board_Text.Length - 1)
                    Clip_Board_Text = Clip_Board_Text & Environment.NewLine
                End If

                'Get rows
                For Each row As DataGridViewRow In dgv.Rows
                    ' If Not row.IsNewRow Then

                    oCurrent_dgv_Col = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)

                    Do
                        If row.Cells(oCurrent_dgv_Col.Index).Value IsNot Nothing Then
                            Clip_Board_Text = Clip_Board_Text & row.Cells(oCurrent_dgv_Col.Index).Value.ToString
                        End If

                        Clip_Board_Text = Clip_Board_Text & Chr(Keys.Tab)
                        oCurrent_dgv_Col = dgv.Columns.GetNextColumn(oCurrent_dgv_Col, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
                    Loop Until oCurrent_dgv_Col Is Nothing

                    Clip_Board_Text = Clip_Board_Text.Substring(0, Clip_Board_Text.Length - 1)
                    Clip_Board_Text = Clip_Board_Text & Environment.NewLine

                    'End If
                Next

                'Puting Text to clipboard
                If Clip_Board_Text.Length > 0 Then
                    Dim dataObj As New DataObject
                    dataObj.SetText(Clip_Board_Text)
                    Clipboard.SetDataObject(dataObj, True)
                End If

            End If

            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error: Copy Text to ClipBoard", MessageBoxButtons.OK)
            Exit Sub
        End Try
    End Sub



End Class
